﻿#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
#include <QPainter>
#include <QPixmap>
#include <QTimer>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->resize(1024,640);
    pixmap = new QPixmap(this->width(), this->height());
    pixmap->fill(Qt::white);
    flag = 0;
    ag = 0;

    startTimer(1000);
}

Widget::~Widget()
{
    delete ui;
}


#if 0
void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    painter.setPen(Qt::DashDotDotLine);
    painter.setPen(Qt::red);  //设置画笔颜色

    painter.drawLine(beginPoint, endPoint);
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    beginPoint = event->pos();
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    endPoint = event->pos();
    update();   //自动触发绘图事件
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    endPoint = event->pos();
    update();   //自动触发绘图事件
}
#else
void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPainter painter1(pixmap);

    if(flag == 1)
    {
        //鼠标移动导致绘图事件,将历史图形pixmap的内容滑到窗体上
        painter.drawPixmap(0, 0, *pixmap);
       // painter.drawLine(beginPoint,endPoint);
        painter1.drawEllipse((beginPoint + endPoint) / 2, 50, 50);
    }
    else if(flag == 2)
    {
        //鼠标松开导致绘图事件，将新的图形画到pixmap上,再将pixmap画到窗体上
        //painter1.drawLine(beginPoint,endPoint);
        painter1.drawEllipse((beginPoint + endPoint) / 2, 50, 50);
        painter.drawPixmap(0, 0, *pixmap);
    }
    else if(flag == 3)
    {
        //实现坐标系原点的移动
        painter.translate(500,320);
        ag += 6;
        ag += (ag + 360) % ag;
        painter.rotate(ag);
        painter.drawLine(0, 0, 500, 320);
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    beginPoint = event->pos();
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    flag = 2;
    endPoint = event->pos();
    update();   //自动触发绘图事件
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    flag = 1;
    endPoint = event->pos();
    update();   //自动触发绘图事件
}

void Widget::timerEvent(QTimerEvent *event)
{
    flag = 3;
    update();
}



#endif
